package com.itheima.controller;

import com.itheima.pojo.*;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {
    //查询班级
    @Autowired
    private ClazzService clazzService;

    @GetMapping
    public Result list(ClazzQueryParam clazzQueryParam) {
        log.info("分页查询班级列表");
        // 使用分页查询方法而不是list方法
        PageResult pageResult = clazzService.page(clazzQueryParam);
        return Result.success(pageResult);
    }

    // 添加查询所有班级的方法
    @GetMapping("/list")
    public Result listAll() {
        log.info("查询所有班级列表");
        List<Clazz> clazzList = clazzService.listAll();
        return Result.success(clazzList);
    }
    // 修复删除方法：使用路径参数
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("根据id删除班级, id: {}", id);
        clazzService.deleteById(id);
        return Result.success();
    }
    @PostMapping
    public Result sava(@RequestBody Clazz clazz) {
        log.info("新增班级, clazz: {}", clazz);
        clazzService.sava(clazz);
        return Result.success();
    }


    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据ID查询, id: {}", id);
        Clazz clazz = clazzService.getById(id);
        return Result.success(clazz);
    }

    @PutMapping
    public Result update(@RequestBody Clazz clazz) {
        log.info("修改班级, clazz: {}", clazz);
        clazzService.update(clazz);
        return Result.success();
    }
}
